package org.nokatag.code;

public class MarbleImageOp extends AbstractTransformImageOp
{

    double scale;
    double amount;
    double turbulence;
    double tx[];
    double ty[];
    double randomX;
    double randomY;

    public MarbleImageOp()
    {
        scale = 15D;
        amount = 1.1000000000000001D;
        turbulence = 6.2000000000000002D;
        randomX = 256D * Math.random();
        randomY = 256D * Math.random();
    }

    public double getScale()
    {
        return scale;
    }

    public void setScale(double scale)
    {
        this.scale = scale;
    }

    public double getAmount()
    {
        return amount;
    }

    public void setAmount(double amount)
    {
        this.amount = amount;
    }

    public double getTurbulence()
    {
        return turbulence;
    }

    public void setTurbulence(double turbulence)
    {
        this.turbulence = turbulence;
    }

    protected synchronized void init()
    {
        tx = new double[256];
        ty = new double[256];
        for(int i = 0; i < 256; i++)
        {
            double angle = (6.2831853071795862D * (double)i * turbulence) / 256D;
            tx[i] = amount * Math.sin(angle);
            ty[i] = amount * Math.cos(angle);
        }

    }

    protected void transform(int x, int y, double t[])
    {
        int d = limitByte((int)(127D * (1.0D + PerlinNoise.noise2D((double)x / scale + randomX, (double)y / scale + randomY))));
        t[0] = (double)x + tx[d];
        t[1] = (double)y + ty[d];
    }

    protected void filter2(int inPixels[], int outPixels[], int width, int height)
    {
        for(int y = 0; y < height; y++)
        {
            for(int x = 0; x < width; x++)
            {
                int pixel = limitByte((int)(127D * (1.0D + PerlinNoise.noise2D((double)x / scale + randomX, (double)y / scale + randomY))));
                outPixels[x + y * width] = limitByte(255) << 24 | limitByte(pixel) << 16 | limitByte(pixel) << 8 | limitByte(pixel);
            }

        }

    }
}

